﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using System.Text;
using VietKinsNutrition.Data;
using DevExpress.XtraEditors;
using VietKinsNutrition.Common;
using DevExpress.XtraGrid;

namespace VietKinsNutrition.Bussiness
{
    public class ClsTblDm_NhapthucphamBLL
    {
        ClsTblDm_NhapthucphamDAL ClsTblDm_Nhapthucpham_dal = new ClsTblDm_NhapthucphamDAL();
        public DataTable Tbl_Tonghopthu_chi(bool Trangthai, DateTime FromNgay, DateTime ToNgay)
        {
            return this.ClsTblDm_Nhapthucpham_dal.Tbl_Tonghopthu_chi(Trangthai, FromNgay, ToNgay);
        }
        public void Bind_CboNhomthucpham(ComboBoxEdit Cbo)
        {
            try
            {
                DataTable Tbl = new DataTable();
                ClsTblDm_Nhomthucpham_ThucanDAL ClsTblDm_Nhomthucpham_Thucandal = new ClsTblDm_Nhomthucpham_ThucanDAL();
                Tbl = ClsTblDm_Nhomthucpham_Thucandal.SelectAll();
                DataRow Rw = Tbl.NewRow();
                Rw["Id"] = 0;
                Rw["Tennhom"] = "*Tất cả*";
                Tbl.Rows.InsertAt(Rw, 0); 
                UserComboBoxItem.SetItem(Cbo, Tbl, "Id", "Tennhom");
            }
            catch { }
        }
        ClsTblDm_NamhocBLL ClsTblDm_Namhocbll = new ClsTblDm_NamhocBLL();
        public void TblNhapthucpham_Del(int HoadonnhapId)
        {
            ClsTblDm_Nhapthucpham_dal.TblNhapthucpham_Del(HoadonnhapId);
        }
        public void TblHoadonnhapxuat_Add(DateTime NgayNX, float Tongtien)
        {
            ClsTblDm_Nhapthucpham_dal.TblHoadonnhapxuat_Add(NgayNX, Tongtien);
        }
        public DataTable SelectAlSohoadonBy(DateTime NgayNX)
        {
            return this.ClsTblDm_Nhapthucpham_dal.SelectAlSohoadonBy(NgayNX);
        }
        public DataTable SelectAlSohoadonByTo_From(DateTime FromNX, DateTime ToNX)
        {
            return this.ClsTblDm_Nhapthucpham_dal.SelectAlSohoadonByTo_From(ToNX, FromNX);
        }
        public DataTable GianhapByNgayNXandThucphamthucanId(DateTime NgayNX, int Thucan_thucphamId)
        {
            return this.ClsTblDm_Nhapthucpham_dal.GianhapByNgayNXandThucphamthucanId(NgayNX, Thucan_thucphamId);
        }
        public void Bind_CboSohoadon(DateTime NgayNX, ComboBoxEdit Cbo)
        {
            //Cbo.DataSource = this.ClsTblDm_Nhapthucpham_dal.TblListSohoadon(NgayNX);
            //Cbo.ValueMember = "Id";
            //Cbo.DisplayMember = "Id";
        }
        public void Bind_GridThucphambyNhomandTenDukien(int NhomthucanId, String Tenthucan, GridControl Grid)
        {
            DataTable Tbl = new DataTable();
            Tbl = this.ClsTblDm_Nhapthucpham_dal.TblListThucpham_thucanByNhomthucphamIdandTenthucan(NhomthucanId, Tenthucan);
            Tbl.Columns["IdThucpham_thucan"].ColumnName = "Id";
            Tbl.Columns.Remove("DonvitinhId");
            Grid.DataSource = Tbl;
        }

        public void Bind_GridThucphambyNhomandTen(int NhomthucanId, String Tenthucan, GridControl Grid)
        {
            Grid.DataSource = this.ClsTblDm_Nhapthucpham_dal.TblListThucpham_thucanByNhomthucphamIdandTenthucan(NhomthucanId, Tenthucan);
        }

        public void Bind_GridThucphambyNhomandTen(String Tenthucan, GridControl Grid)
        {
            Grid.DataSource = this.ClsTblDm_Nhapthucpham_dal.TblListThucpham_thucanByNhomthucphamIdandTenthucan(Tenthucan);
        }

        public void Bind_Grid_ListThucpham(int NhomthucphamId, GridControl Grid)
        {
            Grid.DataSource = this.ClsTblDm_Nhapthucpham_dal.TblListThucpham_thucanByNhomthucphamId(NhomthucphamId);
        }

        // TblHoadonnhapxuat_GetId_ByNgay
        public DataTable TblDanhsachhangtonByFindName(string Name)
        {
            return ClsTblDm_Nhapthucpham_dal.TblDanhsachhangtonByFindName(Name);
        }

        public DataTable TblFindThucphanxuatByThucpham_thucanIdAndDtime(int Thucpham_thucanId, DateTime NgayNX)
        {

            DataTable Tbl = new DataTable();
            Tbl = ClsTblDm_Nhapthucpham_dal.TblDanhsachhangtonById(Thucpham_thucanId, NgayNX);
            return Tbl;
        }

        public DataTable TblFindThucphanxuatByThucpham_thucanIdAndDtime_ALLSchool(int Thucpham_thucanId, DateTime NgayNX)
        {

            DataTable Tbl = new DataTable();
            Tbl = ClsTblDm_Nhapthucpham_dal.TblDanhsachhangtonById_AllSchool(Thucpham_thucanId, NgayNX);
            return Tbl;
        }

        public DataTable TblDanhsachhangtonByFindNameandNgayNX(string Name, DateTime NgayNX)
        {
            return ClsTblDm_Nhapthucpham_dal.TblDanhsachhangtonByFindNameandNgayNX(Name, NgayNX);
        }
        public DataTable TblDanhsachhangton()
        {
            DataTable Tbl = new DataTable();
            Tbl = ClsTblDm_Nhapthucpham_dal.TblDanhsachhangton();
            return Tbl;
        }

        public DataTable TblHoadonnhapxuat_GetId_ByNgay(DateTime NgayNX)
        {
            return ClsTblDm_Nhapthucpham_dal.TblHoadonnhapxuat_GetId_ByNgay(NgayNX);
        }

        public DataTable TblChitietnhapBySohoadonId(int HoadonnhapxuatId)
        {
            try
            {
                DataTable ReturnTbl = new DataTable();
                ReturnTbl = ClsTblDm_Nhapthucpham_dal.TblChitietnhapBySohoadonId(HoadonnhapxuatId);
                ReturnTbl.Columns.Add("Thanhtien", ReturnTbl.Columns["Soluong"].DataType);
                ReturnTbl.Columns.Remove("HoadonnhapxuatId");
                foreach (DataRow Rw in ReturnTbl.Rows)
                {
                    Rw["Thanhtien"] = float.Parse(Rw["Dongia"].ToString()) * float.Parse(Rw["Soluong"].ToString());
                }
                return ReturnTbl;
            }
            catch { return null; }
        }

        public DataTable Table_DanhsachnguyenlieuTonkho(DateTime values)
        {
            ClsTblDm_XuatkhoBLL ClsTblDm_Xuatkhobll = new ClsTblDm_XuatkhoBLL();
            DataTable TblTonkho = ClsTblDm_Xuatkhobll.GetTbl_TblNguyenlieutonkho();

            return TblTonkho;
        }
        public void ExportFileExel(DataGridView tblNhanvienDataGridView, SaveFileDialog File, TextBox txt, int SHD)
        {
            string sTenso = null, sTentruong = null, sTenphong = null, sTieude = null;

            ClsExport2ExcelBLL ClsExport2Excelbll = new ClsExport2ExcelBLL();
            DataTable dt;
            ClsTblDmTruonghocDAL ClsTblDmTruonghocdal = new ClsTblDmTruonghocDAL();
            dt = ClsTblDmTruonghocdal.TblTruong();
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                sTenso = dr["TensoGD"].ToString();
                sTentruong = dr["Tentruong"].ToString();
                sTenphong = dr["TenphongGD"].ToString();
                sTieude = " HÓA ĐƠN NHẬP HÀNG ";
                File.FileName = sTieude;
            }
            ClsExport2Excelbll.ExportFileExel(SHD, sTieude, sTenso, sTenphong, sTentruong, ".xls", tblNhanvienDataGridView, txt);
        }
    }
}
